﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace MII_FIF.Layouts.MII_FIF.Checklist
{
    public partial class ViewCHNetworkDevice : LayoutsPageBase
    {
        SqlConnection conn;
        DataTable dt;
        SqlDataAdapter adapt;
        //string connStr = "Data Source=sp2013-server;Initial Catalog=InventoryManagement;User ID=sa;Password=P@ssw0rd";
        string queryStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.ChecklistGroupName))
            {
                btnDelete.Click += btnDelete_Click;
                btnAdd.Click += btnAdd_Click;
                GridNet.RowCommand += GridVM_RowCommand;

                if (!IsPostBack)
                {
                    initialize();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        protected DataTable ExecuteDataTable(string queryStr)
        {
            //conn = new SqlConnection(connStr);
            conn = new SqlConnection(Configuration.DBConnection());
            conn.Open();
            dt = new DataTable();
            adapt = new SqlDataAdapter(queryStr, conn);
            adapt.Fill(dt);
            conn.Dispose();
            return dt;
        }

        void initialize()
        {
            divstatus.Visible = false;

            if (Request.QueryString["Status"] == "Submitted")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Data has been Submitted";
                divstatus.Visible = true;
            }
            if (Request.QueryString["Status"] == "Removed")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Data has been Removed";
                divstatus.Visible = true;
            }
            LoadNet();
        }

        void LoadNet()
        {
            try
            {

                queryStr = @"EXEC SP_SelectDataCHNet";
                dt = ExecuteDataTable(queryStr);
                GridNet.DataSource = dt;
                GridNet.DataBind();

                ViewState["DTNet"] = dt;
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_layouts/MII_FIF/Checklist/FormCHNetworkDevice.aspx"));
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            bool delete = false;
            DataTable DTNet = ((DataTable)ViewState["DTNet"]);
            foreach (GridViewRow row in GridNet.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }

            }
            if (delete)
            {
                for (int i = GridNet.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridNet.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {
                            queryStr = @"SP_DeleteDataCHNet '" + DTNet.Rows[i]["ID"].ToString() + "'";
                            ExecuteDataTable(queryStr);
                            DTNet.Rows.RemoveAt(i);
                        }
                }
                GridNet.DataSource = DTNet;
                GridNet.DataBind();
                ViewState["DTNet"] = DTNet;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }

        }

        void GridVM_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Checklist/FormCHNetworkDevice.aspx?ID=" + e.CommandArgument));
            }
        }

        protected void GridNet_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            GridNet.PageIndex = e.NewPageIndex;
            LoadNet();
        }


    }
}
